const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
// pagesA/servedetail/servedetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    show: false,
    id: '',
    info: '',
    goods_sku_id: '',
    num: 1,
    skill_id: '',
    safeBottom: `30px`
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  collect(){
    http.get('follow/handlefollow', {
      follow_id: this.data.id,
      type: 1,
      state: this.data.info.followState ? 0 : 1
    }).then(res => {
      this.data.info.followState = this.data.info.followState ? 0 : 1
      this.setData({
        info: this.data.info
      })
    })
  },
  chooseSku(e) {
    this.setData({
      goods_sku_id: e.currentTarget.dataset.id
    })
  },
  add() {
    this.setData({
      num: ++this.data.num
    })
  },
  sub() {
    if (this.data.num === 1) {
      return
    }
    this.setData({
      num: ++this.data.num
    })
  },
  openshow() {
    if(this.data.info.spec_type === 1){
      this.setData({
        show: true
      });
    }else {
      if (this.data.info.choose_skill_type === 1) {
        if (this.data.skill_id) {
          util.skip('/service/makeorder/makeorder?goods_sku_id=' + this.data.goods_sku_id + '&num=' + this.data.num + '&goods_id=' + this.data.id + '&skill_id' + this.data.skill_id)
  
        } else {
          util.skip('/service/servelist/servelist?goods_sku_id=' + this.data.goods_sku_id + '&num=' + this.data.num + '&goods_id=' + this.data.id)
        }
  
      } else {
        if (this.data.skill_id) {
          util.skip('/service/makeorder/makeorder?goods_sku_id=' + this.data.goods_sku_id + '&num=' + this.data.num + '&goods_id=' + this.data.id + '&skill_id' + this.data.skill_id)
        } else {
          util.skip('/service/makeorder/makeorder?goods_sku_id=' + this.data.goods_sku_id + '&num=' + this.data.num + '&goods_id=' + this.data.id)
        }
  
      }
    }
    
  },

  preimg() {
    wx.previewImage({
      current: '../../static/index/bg.png',
      urls: ['../../static/index/bg.png'],
    })
  },


  bindchange(event) {
    this.setData({
      current: event.detail.current
    })
  },
  getInfo() {
    http.get('goods/goodsinfo', {
      id: this.data.id
    }).then(res => {
      this.setData({
        info: res.data,
        goods_sku_id: res.data.sku[0] ? res.data.sku[0].id : ''
      })
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const high = app.globalData.safeBottom
    this.setData({
      safeBottom: `${high}px`
    })
    this.setData({
      id: options.id
    })
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log('我下拉了')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    console.log('我下拉了')
  }
})